//============================================================================================================================
// CLOUD FUNCTIONS
//============================================================================================================================

// Use Parse.Cloud.define to define as many cloud functions as you want.

// For example:

Parse.Cloud.define("hello", function(request, response) {

  console.log("--Parse.User.current()-- " + request.user);

  response.success("Hello world!");

});



// Use Parse.Cloud.define to define as many cloud functions as you want.

// For example:

Parse.Cloud.define("testUser", function (req, response) {

    if (req.user) {

        Parse.Cloud.useMasterKey();

        req.user.fetch({

            success: function (user) {

                response.success(user._sessionToken);

            },

            error: function (user, err) {

                response.error(err.message);

            }

        });

    } else {

        response.error("Not logged in.");

    }

});



// Use Parse.Cloud.define to define as many cloud functions as you want.

// For example:

Parse.Cloud.define("getMyUserID", function (req, response) {

    if (req.user) {

        req.user.fetch({

            success: function (user) {

                response.success(user.id);

            },

            error: function (user, err) {

                response.error(err.message);

            }

        });

    } else {

        response.error("Not logged in.");

    }

});

	function funcionMagicaDeLucho(obj){

		for (var p in obj){
			
			if (typeof( obj[p] ) != "function") {
				console.log(p + " >> "+ obj[p]);
			}
			
		}

	}







// ESTA FUNCTION ACTUALIZA EL CAMPO UNREAD PAYMENTS EN PROFILES

function updateUnreadPayments(req, response) 
{

	// console.log("price >>>> " + req.get("itm_price"));
	//console.log("seller! >>>> " + req.get("seller").id);
	// console.log("status >>>> " + req.get("status"));

	// funcionMagicaDeLucho(req.get("seller"));

	var query = new Parse.Query("User");
	var user = query.get(req.get("seller").id);
	//.equalTo("id",req.get("seller").id);

	console.log("query>>>>>>> " + user);

	funcionMagicaDeLucho(user);
	//1 - param object Transaction - status {aproved, refund}



	//2 - llamar cloud funtion getMyUserID()

	/*

	Parse.Cloud.run('getMyUserID', { movie: 'The Matrix' }, {

	  success: function(ratings) {

		// ratings should be 4.5

	  },

	  error: function(error) {

	  }

	});

	*/



 

	//3 - select de profile filtrando por ID



	//4 - change array field for IDs



	//5 - update de profile con el nuevo ID de transaction

}



//=====================================================================================================================================

// TRIGGERS FOR TRANSACTIONS

//=====================================================================================================================================



Parse.Cloud.afterSave("Transactions", function(request) 

{

	// calls 1

/*
	Parse.Cloud.run('updateUnreadPayments', {

		success: function(ratings) {

			// ratings should be 4.5

		},

		error: function(error) {

		

		}

	});
*/

	// console.log("Bruno's log >>>> " + request.object.get("itm_price"));

	updateUnreadPayments(request.object);

/*	Parse.Cloud.run('updateUnreadPayments',
		{ object: request.object},
		{
  		success: function(result) { 
  				console.log(result); },
  		error: function(error) {
  				console.log(error); }
		}
	);
*/
	// calls 2



	// calls 3



	// calls 4

});